package com.jinghao.tws.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.jinghao.core.common.exception.JingHaoCustomerException;
import com.jinghao.core.common.service.impl.JingHaoBaseServiceImpl;
import com.jinghao.core.common.util.cnd.JhCnd;
import com.jinghao.core.common.util.cnd.JhUpdate;
import com.jinghao.tws.entity.WvAutoUploadCloudConfig;
import com.jinghao.tws.entity.WvCamera;
import com.jinghao.tws.entity.WvUploadTaskInfo;
import com.jinghao.tws.entity.WvCamera.EWvCamera;
import com.jinghao.tws.entity.WvUploadTaskInfo.EWvUploadTaskInfo;
import com.jinghao.tws.service.IWvAutoUploadCloudConfigService;
import com.jinghao.tws.service.IWvCameraService;
import com.jinghao.tws.service.IWvFlowService;
import com.jinghao.tws.service.IWvUploadTaskInfoService;
import com.jinghao.tws.util.EJhTwsCode;

@Service("wvCameraService")
public class WvCameraServiceImpl extends JingHaoBaseServiceImpl<WvCamera> implements IWvCameraService {
	//==================定制内容开始======================
	@Resource
	private IWvFlowService wvFlowService;
	
	@Resource
	private IWvAutoUploadCloudConfigService wvAutoUploadCloudConfigService;
	
	@Resource
	private IWvUploadTaskInfoService wvUploadTaskInfoService;
	
	@Override
	public void listWvCamera(WvCamera model) {
		JhCnd cnd=new JhCnd();
		if (!StringUtils.isEmpty(model.getDeviceName())) {
			model.setDeviceName("%"+model.getDeviceName()+"%");
			cnd.andLike("deviceName");
		}
		if (!StringUtils.isEmpty(model.getDeviceId())) {
			cnd.andEquals("deviceId");
		}
		if (!StringUtils.isEmpty(model.getCameraUuid())) {
			cnd.andEquals("cameraUuid");
		}
		model.lnkCnd(cnd); 
		model.setMapList(this.queryMapList(model));
		
	}
	
	@Override
	public void listWvCameraAll(WvCamera model) {
		JhCnd cnd=new JhCnd();
		if (!StringUtils.isEmpty(model.getDeviceName())) {
			model.setDeviceName("%"+model.getDeviceName()+"%");
			cnd.andLike("deviceName");
		}
		if (!StringUtils.isEmpty(model.getDeviceId())) {
			cnd.andEquals("deviceId");
		}
		if (!StringUtils.isEmpty(model.getCameraUuid())) {
			cnd.andEquals("cameraUuid");
		}
	    cnd.andEquals("status");
	    model.setStatus(EWvCamera.status_1.getKey());
		model.lnkCnd(cnd); 
		model.setPage(null);
		model.setMapList(this.queryMapListById(model, "queryMapList"));
		
	}
	@Override
	public void addWvCamera(WvCamera model) {
		 this.checkWvCameraInfo(model);
		 model.setCameraUuid(model.getUUID());
		 model.setCreator("  ");
		 model.setCreateTime(new Date());
		 model.setStatus(EWvCamera.status_1.getKey());
		 JhCnd cnd=new JhCnd();
		 cnd.andEquals("deviceId");
		 model.lnkCnd(cnd);
		 List<Map<String,Object>> list=this.queryMapList(model);
		 if(list!=null&&list.size()>0){
			 throw new JingHaoCustomerException(EJhTwsCode.TWS2001.getCode(),"你提供的设备ID已存在，请重新输入！");
		 }
		 this.add(model);
		 //生成上传任务
		 this.addWvUploadTaskInfo(model);
	}

	public void checkWvCameraInfo(WvCamera model){
		Map<String,Object> map=model.getObjMap();
		if(map==null){
			throw new JingHaoCustomerException(EJhTwsCode.TWS1001.getCode(),"请求参数有误 ！");
		}
		if (map.get("cameraUuid")==null||StringUtils.isEmpty(map.get("cameraUuid").toString())) {
			model.setCameraUuid("");
		}else{
			model.setCameraUuid(map.get("cameraUuid").toString());
		}	
		if (map.get("deviceId")==null||StringUtils.isEmpty(map.get("deviceId").toString())) {
			throw new JingHaoCustomerException(EJhTwsCode.TWS2001.getCode(),"请输入设备ID！");
		}
		model.setDeviceId(map.get("deviceId").toString());
		if (map.get("deviceName")==null||StringUtils.isEmpty(map.get("deviceName").toString())) {
			throw new JingHaoCustomerException(EJhTwsCode.TWS2001.getCode(),"请输入设备名称！");
		}
		model.setDeviceName(map.get("deviceName").toString());
		if (map.get("channelId")==null||StringUtils.isEmpty(map.get("channelId").toString())) {
			throw new JingHaoCustomerException(EJhTwsCode.TWS2001.getCode(),"请输入存储ID！");
		}
		model.setChannelId(map.get("channelId").toString());
		if (map.get("sort")==null||StringUtils.isEmpty(map.get("sort").toString())) {
			throw new JingHaoCustomerException(EJhTwsCode.TWS2001.getCode(),"请输入排序号！");
		}
		model.setSort(Integer.parseInt(map.get("sort").toString()));
		
		if (map.get("remark")==null) {
			model.setRemark("");
		}else{
			model.setRemark(map.get("remark").toString());
		}
		
		if (map.get("address")==null) {
			model.setAddress("");
		}else{
			model.setAddress(map.get("address").toString());
		}
	}
	
	@Override
	public void updateWvCamera(WvCamera model) {
		this.checkWvCameraInfo(model);
		if (StringUtils.isEmpty(model.getCameraUuid())) {
			throw new JingHaoCustomerException(EJhTwsCode.TWS2001.getCode(),"请输入设备UUID！");
		}
		 JhCnd cnd=new JhCnd();
		 cnd.andNotEquals("cameraUuid");
		 cnd.andEquals("deviceId");
		 model.setJhCnd(cnd);
		 List<Map<String,Object>> list=this.queryMapList(model);
		 if(list!=null&&list.size()>0){
			 throw new JingHaoCustomerException(EJhTwsCode.TWS2001.getCode(),"你提供的设备ID已存在，请重新输入！");
		 } 
		 
		 JhUpdate update=new JhUpdate();
		 update.add("address").add("channelId").add("deviceId").add("sort").add("deviceName").add("updater").add("updateTime");
		 update.add("remark");
		 model.setUpdater(" ");
		 model.setUpdateTime(new Date());
		 model.setUpdateSet(update);	
		 
		 JhCnd cnd2=new JhCnd();
		 cnd2.andEquals("cameraUuid");
		 model.lnkCnd(cnd2);
		 this.updateSame(model);
		 
		 //生成上传任务
		 this.addWvUploadTaskInfo(model);
	}

	@Override
	public void updateWvCameraState(WvCamera model) {
		if (model.getStatus()==null) {
			throw new JingHaoCustomerException(EJhTwsCode.TWS2001.getCode(),"请提供设备状态信息！");
		}
		if(model.getStrList()==null||model.getStrList().size()<=0){
			throw new JingHaoCustomerException(EJhTwsCode.TWS2001.getCode(),"请选择相关设备记录信息！");
		}
		
		
		
		JhCnd cnd=new JhCnd();
		//若进行删除，则进行删除操作
		if(model.getStatus().equals(EWvCamera.status_2.getKey())){
			 //第一步：先判定配置表是否存在引用。queryMapListCameraAndProcessConfigCheck
			cnd.andIn("cameraUuid", "strList", model.getStrList());
			model.lnkCnd(cnd);
			List<Map<String,Object>> list=this.queryMapListById(model, "queryMapListCameraAndProcessConfigCheck");
			if(list!=null&&list.size()>0){
				throw new JingHaoCustomerException(EJhTwsCode.TWS2001.getCode(),"摄像机正在环节使用中，请先取消再删除！");
			}else{
				this.deleteSame(model);
			}
			return;
		}
		cnd.reset();
		cnd.andIn("cameraUuid", "strList", model.getStrList());
		if(model.getStatus().equals(EWvCamera.status_0.getKey())){
			cnd.append(" and status=1");
		}else if(model.getStatus().equals(EWvCamera.status_1.getKey())){
			cnd.append(" and status=0");
		}else if(model.getStatus().equals(EWvCamera.status_2.getKey())){
			 //删除所有记录
		}else{
			throw new JingHaoCustomerException(EJhTwsCode.TWS2001.getCode(),"提供设备状态信息有误！");
		}
		model.lnkCnd(cnd);
		model.setUpdateTime(new Date());
		JhUpdate update=new JhUpdate();
		update.add("status").add("updateTime");
		model.setJhUpdate(update);
		this.updateSame(model);	
	}
	public void addCloudTask(){
		Set<String> dateSet=new HashSet<String>();
		List<String> processCodeList=new ArrayList<String>();
		WvAutoUploadCloudConfig wvAutoUploadCloudConfig=new WvAutoUploadCloudConfig();
		List<Map<String,Object>> list=this.wvAutoUploadCloudConfigService.queryMapList(wvAutoUploadCloudConfig);
		for(Map<String,Object> map:list){
			if(map.get("processCode")!=null){
				dateSet.add(map.get("processCode").toString());
				processCodeList.add(map.get("processCode").toString());
			}
		}
	}
	
	public void addWvUploadTaskInfo(WvCamera model){
		
		WvUploadTaskInfo wvTaskInfo=new WvUploadTaskInfo();
		//删除任务
		JhCnd cnd=new JhCnd();
		wvTaskInfo.setWorkUuid(model.getCameraUuid());
		wvTaskInfo.setTaskType(EWvUploadTaskInfo.taskType_1.getKey());
		cnd.andEquals("taskType");
		//cnd.andEquals("workUuid");
		wvTaskInfo.setJhCnd(cnd);
		this.wvUploadTaskInfoService.deleteSame(wvTaskInfo);
		//保存视频任务生成
		wvTaskInfo.setTaskUuid(model.getUUID());
		wvTaskInfo.setCreateTime(new Date());
		wvTaskInfo.setTaskType(EWvUploadTaskInfo.taskType_1.getKey());
		wvTaskInfo.setWorkUuid(model.getCameraUuid());
		wvTaskInfo.setTaskLevel(EWvUploadTaskInfo.taskType_1.getKey());
		this.wvUploadTaskInfoService.add(wvTaskInfo);
	}
	//==================定制内容结束======================
}
